package kr.ac.kongju.gedl.common.util;

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;


/**
 * 날짜를 쉽게 쪼물락거리기 위한 함수군
 */
public class DateUtil {
	/**
	 * 음력을 계산하기 위한 날짜 수 테이블.
	 * 윤달이 포함 될 수 있으므로 달의 갯수는 12가 아니라 13이다.
	 * 테이블에 포함된 음수는 윤달임을 의미한다.
	 * 즉 첨자 6에 -29라는 데이터가 있다면 6월에 해당하는 윤달이며
	 * 달의 길이는 29라는 의미이다. 양수일 경우에는 첨자 6은 7월을 의미한다.
	 * 첨자가 0에서 시작한다는 것을 생각하면 쉽게 이해할 수 있을 것이다.
	 */
	public final static int lunar[][] = {
		{29, 30, 29, 30, 29, 30, 30,-29, 30, 30, 29, 30, 29}, // 1881
		{29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 00}, // 1882
		{29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 00}, // 1883
		{30, 29, 29, 30, 29,-29, 30, 29, 30, 30, 29, 30, 30}, // 1884
		{30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 00}, // 1885
		{30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 1886
		{30, 30, 29, 30,-29, 30, 29, 29, 30, 29, 30, 29, 30}, // 1887
		{30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 00}, // 1888
		{30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 00}, // 1889
		{29, 30,-29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30}, // 1890
		{29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 00}, // 1891
		{29, 29, 30, 29, 29, 30,-29, 30, 30, 29, 30, 30, 30}, // 1892
		{29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 00}, // 1893
		{29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 00}, // 1894
		{30, 29, 30, 29, 30,-29, 29, 30, 29, 30, 29, 30, 29}, // 1895
		{30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 1896
		{29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 00}, // 1897
		{30, 29, 30,-29, 30, 30, 29, 30, 29, 30, 29, 30, 29}, // 1898
		{30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 00}, // 1899
		{29, 30, 29, 29, 30, 29, 30, 30,-29, 30, 30, 29, 30}, // 1900
		{29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 00}, // 1901
		{30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 00}, // 1902
		{29, 30, 29, 30, 29,-29, 30, 29, 29, 30, 30, 29, 30}, // 1903
		{30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 00}, // 1904
		{30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 1905
		{29, 30, 30, 29,-30, 29, 30, 29, 30, 29, 30, 29, 30}, // 1906
		{29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 00}, // 1907
		{30, 29, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 00}, // 1908
		{29, 30,-29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30}, // 1909
		{29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 00}, // 1910
		{30, 29, 30, 29, 29, 30,-29, 29, 30, 30, 29, 30, 30}, // 1911
		{30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 00}, // 1912
		{30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 00}, // 1913
		{30, 30, 29, 30, 30,-29, 29, 30, 29, 30, 29, 29, 30}, // 1914
		{30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 00}, // 1915
		{29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 00}, // 1916
		{30, 29,-29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29}, // 1917
		{30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30, 00}, // 1918
		{29, 30, 29, 29, 30, 29, 30,-29, 30, 30, 29, 30, 30}, // 1919
		{29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 00}, // 1920
		{30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 00}, // 1921
		{30, 29, 30, 30, 29,-29, 30, 29, 29, 30, 29, 30, 30}, // 1922
		{29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 00}, // 1923
		{30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 00}, // 1924
		{30, 29, 30, 30,-29, 30, 29, 30, 30, 29, 30, 29, 30}, // 1925
		{29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 00}, // 1926
		{30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 00}, // 1927
		{29, 30,-29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30}, // 1928
		{29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 00}, // 1929
		{29, 30, 30, 29, 29, 30,-29, 29, 30, 29, 30, 30, 29}, // 1930
		{30, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 00}, // 1931
		{30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 00}, // 1932
		{29, 30, 30, 29, 30,-30, 29, 30, 29, 30, 29, 29, 30}, // 1933
		{29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 00}, // 1934
		{29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 00}, // 1935
		{30, 29, 29,-30, 29, 30, 29, 30, 29, 30, 30, 30, 29}, // 1936
		{30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 00}, // 1937
		{30, 30, 29, 29, 30, 29, 29,-30, 29, 30, 30, 29, 30}, // 1938
		{30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 1939
		{30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 00}, // 1940
		{30, 30, 29, 30, 30, 29,-30, 29, 29, 30, 29, 30, 29}, // 1941
		{30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 00}, // 1942
		{29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 00}, // 1943
		{29, 29, 30, 29,-30, 29, 30, 29, 30, 30, 29, 30, 30}, // 1944
		{29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 00}, // 1945
		{30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 00}, // 1946
		{30, 30,-29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30}, // 1947
		{30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 1948
		{30, 30, 29, 30, 29, 30, 29,-29, 30, 29, 30, 29, 30}, // 1949
		{30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 00}, // 1950
		{30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 00}, // 1951
		{29, 30, 29, 30, 29,-30, 30, 29, 30, 29, 30, 29, 30}, // 1952
		{29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 30, 30, 00}, // 1953
		{29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 00}, // 1954
		{30, 29, 29,-30, 29, 29, 30, 29, 30, 29, 30, 30, 30}, // 1955
		{29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 00}, // 1956
		{30, 29, 30, 29, 30, 29, 29, 30,-29, 30, 29, 30, 30}, // 1957
		{29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 1958
		{29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 00}, // 1959
		{30, 29, 30, 29, 30, 30,-29, 30, 29, 30, 29, 30, 29}, // 1960
		{30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 00}, // 1961
		{29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 00}, // 1962
		{30, 29, 30, 29,-29, 30, 29, 30, 29, 30, 30, 30, 29}, // 1963
		{30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 00}, // 1964
		{29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 00}, // 1965
		{30, 30, 30,-29, 30, 29, 29, 30, 29, 29, 30, 30, 29}, // 1966
		{30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 1967
		{29, 30, 30, 29, 30, 29, 30,-29, 30, 29, 30, 29, 30}, // 1968
		{29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 29, 29, 00}, // 1969
		{30, 29, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 00}, // 1970
		{29, 30, 29, 29, 30,-29, 30, 29, 30, 30, 30, 29, 30}, // 1971
		{29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 00}, // 1972
		{30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 30, 29, 00}, // 1973
		{30, 30, 29, 30,-29, 29, 30, 29, 29, 30, 30, 29, 30}, // 1974
		{30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 00}, // 1975
		{30, 30, 29, 30, 29, 30, 29, 30,-29, 30, 29, 29, 30}, // 1976
		{30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 00}, // 1977
		{30, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 00}, // 1978
		{30, 29, 29, 30, 29, 30,-30, 29, 30, 30, 29, 30, 29}, // 1979
		{30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 00}, // 1980
		{29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 00}, // 1981
		{30, 29, 30, 29,-29, 30, 29, 29, 30, 30, 29, 30, 30}, // 1982
		{30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 00}, // 1983
		{30, 29, 30, 30, 29, 29, 30, 29, 29, 30,-29, 30, 30}, // 1984
		{29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 00}, // 1985
		{29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 00}, // 1986
		{30, 29, 30, 30, 29, 30,-29, 30, 30, 29, 30, 29, 30}, // 1987
		{29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 00}, // 1988
		{30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 00}, // 1989
		{29, 30, 29, 29, 30,-29, 29, 30, 29, 30, 30, 30, 30}, // 1990
		{29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 00}, // 1991
		{29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 00}, // 1992
		{29, 30, 30,-29, 30, 29, 30, 29, 29, 30, 29, 30, 29}, // 1993
		{30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 00}, // 1994
		{29, 30, 30, 29, 30, 30, 29, 30,-29, 30, 29, 29, 30}, // 1995
		{29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 00}, // 1996
		{29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 00}, // 1997
		{30, 29, 29, 30, 29,-29, 30, 30, 29, 30, 30, 30, 29}, // 1998
		{30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 00}, // 1999
		{30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 00}, // 2000
		{30, 30, 30, 29,-29, 30, 29, 29, 30, 29, 30, 29, 30}, // 2001
		{30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 00}, // 2002
		{30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 00}, // 2003
		{29, 30,-29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 30}, // 2004
		{29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 00}, // 2005
		{29, 29, 30, 29, 30, 29, 30,-29, 30, 30, 29, 30, 30}, // 2006
		{29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 00}, // 2007
		{30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 00}, // 2008
		{30, 30, 29, 29, 30,-29, 29, 30, 29, 30, 29, 30, 30}, // 2009
		{30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 2010
		{30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 00}, // 2011
		{30, 29, 30,-30, 30, 29, 30, 29, 29, 30, 29, 30, 29}, // 2012
		{30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 00}, // 2013
		{29, 30, 29, 30, 29, 30, 29, 30, 30,-29, 30, 29, 30}, // 2014
		{29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 30, 00}, // 2015
		{29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 00}, // 2016
		{30, 29, 29, 30, 29,-29, 30, 29, 30, 29, 30, 30, 30}, // 2017
		{29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 00}, // 2018
		{30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 00}, // 2019
		{30, 29, 30, 30,-29, 30, 29, 29, 30, 29, 30, 29, 30}, // 2020
		{29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 00}, // 2021
		{30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 00}, // 2022
		{29, 30,-29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30}, // 2023
		{29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 00}, // 2024
		{30, 29, 30, 29, 29, 30,-29, 30, 29, 30, 30, 30, 29}, // 2025
		{30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 00}, // 2026
		{29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 30, 00}, // 2027
		{29, 30, 30, 29, 30,-29, 29, 30, 29, 29, 30, 30, 29}, // 2028
		{30, 30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 30, 00}, // 2029
		{29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 00}, // 2030
		{30, 29, 30,-29, 30, 29, 30, 30, 29, 30, 29, 30, 29}, // 2031
		{30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 00}, // 2032
		{29, 30, 29, 29, 30, 29, 30,-29, 30, 30, 30, 29, 30}, // 2033
		{29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 00}, // 2034
		{30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 00}, // 2035
		{30, 30, 29, 30, 29, 29,-30, 29, 29, 30, 29, 30, 30}, // 2036
		{30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 00}, // 2037
		{30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 29, 00}, // 2038
		{30, 30, 29, 30, 30,-29, 30, 29, 30, 29, 30, 29, 29}, // 2039
		{30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 00}, // 2040
		{30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 00}, // 2041
		{29, 30,-29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30}, // 2042
		{29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 00}, // 2043
		{30, 29, 30, 29, 29, 30, 29,-29, 30, 29, 30, 30, 30}, // 2044
		{30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 00}, // 2045
		{30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 00}, // 2046
		{30, 29, 30, 30, 29,-30, 29, 30, 29, 29, 30, 29, 30}, // 2047
		{29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 00}, // 2048
		{30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 00}, // 2049
		{30, 29, 29,-30, 29, 30, 29, 30, 30, 29, 30, 30, 29}  // 2050
	};

	/**
	* 양력 date를 음력으로 환산한다.
	* 아직 윤달에 대한 정보를 반환 받는 방법을 고려 하지 않았다.
	* 향후 별도의 클래스로 분리하는 것이 좋을 듯
	*/
	public static String toLunar(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date base = sdf.parse("18810130"); // 테이블의 시작일을 음력으로 생각하여 양력으로 환산한 날짜
		long offset = base.getTime() / (1000*60*60*24);
		long days = date.getTime() / (1000*60*60*24);
		int y, m=0, d=0;

		for (y = 0; y < lunar.length; y++) {
			for (m = 0; m < 13; m++) {
				if (days < offset+Math.abs(lunar[y][m])) break;
				offset += Math.abs(lunar[y][m]);
			}
			if (m != 13) break;
		}
		for (int i = 0; i <= m; i++) if (lunar[y][i] < 0) m--; // 달의 앞쪽에 윤달이 포진해 있다면 월 인덱스 감소
		y += 1881;
		m++;
		d = (int)(days-offset + 1);
		return String.valueOf(y*1000 + m*100 + d);
	}

	/**
	 * 날짜에서 날짜를 뺀 값 즉 기간을 반환한다.
	 * @param c1 기준일
	 * @param c2 기준일에서 부터 뺄 날짜
	 * @return long 두 날짜 사이의 기간, 같은 날짜인 경우엔 0이된다.
	 */
	public static long distance(Calendar c1, Calendar c2) {
		return (c1.getTime().getTime() - c2.getTime().getTime()) / (1000*60*60*24);
	}

	/**
	 * 날짜에서 날짜를 뺀 값 즉 기간을 반환한다.
	 * @param c1 기준일
	 * @param c2 기준일에서 부터 뺄 날짜
	 * @return long 두 날짜 사이의 기간, 같은 날짜인 경우엔 0이된다.
	 */
	public static long distance(String date1, String date2) throws ParseException {
		 return distance(DateUtil.date("yyyyMMdd", date1), DateUtil.date("yyyyMMdd", date2));
	}

	public static int distance(java.sql.Date date1, java.sql.Date date2) {
		return distance(covertUtilDate(date1), covertUtilDate(date2));
	}

	public static int distance(java.util.Date date, java.util.Date minusDate) {
		long l = date.getTime() - minusDate.getTime();
		return (int)(l / 0x5265c00L);
	}

	public static int getYear(String value) {
		if(value==null || value.trim().length() == 0) return 0;
		
		int year=0;
		String yyyy = value.substring(0,4);
		year = Integer.parseInt(yyyy.trim());
		return year;
	}

	public static int getLastDayInMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		
		cal.set(year, month-1,1);
		int maxday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		return maxday;
	}
	public static String currentdate() {
		String[] week={"일","월","화","수","목","금","토"};
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR) + "." + (c.get(Calendar.MONTH)+1) + "." + c.get(Calendar.DATE) + "  " + week[c.get(Calendar.DAY_OF_WEEK)-1];
	}

	public static String getKorDate(long time) {
		String[] week={"일","월","화","수","목","금","토"};
		Calendar c = Calendar.getInstance();
		c.setTime(new Date(time));
		return c.get(Calendar.YEAR) + "년 " + (c.get(Calendar.MONTH)+1) + "월 " + c.get(Calendar.DATE) + "일(" +week[c.get(Calendar.DAY_OF_WEEK)-1]+ ") " + c.get(Calendar.HOUR_OF_DAY) + "시 " + c.get(Calendar.MINUTE) + "분 " + c.get(Calendar.SECOND) + "초 ";
	}

	public static String today() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR) + String.format("%2.2d", (c.get(Calendar.MONTH)+1)) + String.format("%2.2d", c.get(Calendar.DATE));
	}

	public static String privday(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, day);
		return c.get(Calendar.YEAR) + String.format("%2.2d", (c.get(Calendar.MONTH)+1)) + String.format("%2.2d", c.get(Calendar.DATE));
	}

	public static String getYYYY() {
		return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
	}

	public static String getMM() {
		return String.format("%2.2d", (Calendar.getInstance().get(Calendar.MONTH)+1));
	}

	public static String getDD() {
		return String.format("%2.2d", (Calendar.getInstance().get(Calendar.DATE)));
	}

	/**
	 * 날짜를 SimpleDateFomat의 patten을 적용시켜 문자열로 만들어 반환한다.
	 */
	public static String dateFormat(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		if(date != null) {
			return sdf.format(date);
		}
		else {
			return "";
		}
	}
/*
	public static String dateFormat(java.sql.Date date, String pattern) {
		return dateFormat(covertUtilDate(date), pattern);
	}
*/
	/**
	 *  문자열에 날짜포맷팅을 적용시켜서  날자형으로 변환한다.
	 * @param format  {@link java.text.SimpleDateFormat}의 날짜 포맷팅 문자열
	 * @param value 변환할 문자열 형태의 날짜
	 * @return 변환된 날짜를 반환한다.
	 * @exception ParseException 날짜 변환이 실패한 경우에 발생한다.
	 */
	public static Date date(String format, String value) throws ParseException {
		if (value == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(value);
	}

	public static int getYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	public static String regDate(Date date) {
		if(date != null) return dateFormat(date, "yyyy-MM-dd");
		return null;
	}

	public static String regDate2(java.sql.Date date) {
		if(date != null) return dateFormat(date, "yyyy-MM-dd");
		return null;
	}


	public static String getNowDate() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR) + String.format("%2.2d", (c.get(Calendar.MONTH)+1)) + String.format("%2.2d", c.get(Calendar.DATE));
	}

	public static String getNowTime() {
		Calendar c = Calendar.getInstance();
		return String.format("%2.2d", (c.get(Calendar.HOUR_OF_DAY))) + String.format("%2.2d", (c.get(Calendar.MINUTE))) + String.format("%2.2d", (c.get(Calendar.SECOND)));
	}

	/**
	 * 오늘의 날짜를 java.sql.Date 를 준다.
	 * @return 시, 분, 초, 1/100초가 0으로 초기화된 날짜.
	 */
	public static java.util.Date todayDate() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		/*
		 * net.sf.json.JSONObject 에서 내부적으로 사용하는 BeanPropertyUtil에서
		 * Date형에 대해 getHours() 메소드를 호출하는데, java.sql.Date를 사용하면
		 * 해당 메소드(getHours())가 존재하지 않아 오류가 발생하게 된다.
		 */
//		java.sql.Date today = new java.sql.Date(cal.getTime().getTime());
		java.util.Date today = new java.util.Date(cal.getTime().getTime());
		
		return today;
	}
	
	/**
	 * 오늘 날짜를 반환한다.(어제 날짜에 23시 59분 59초 999ms으로 설정한다.)
	 * @return 시, 분, 초, 1/100가 최대치로 초기화된 날짜
	 */
	public static Date todayFullTimeDate(){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		cal.set(Calendar.HOUR, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);

		java.sql.Date today = new java.sql.Date(cal.getTime().getTime());
		return today;
	}

	/**
	 * 오늘 날짜에 인자값만큼 더한 후 java.sql.Date형을 반환한다.
	 * 반환시 시간, 분, 초, MILLISECOND는 0으로 초기화 된값이다.
	 * @param plus : 오늘 날짜에 더할 값.
	 * @return
	 */
	public static java.util.Date todayDatePlus(int plus) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, plus);

		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		/*
		 * net.sf.json.JSONObject 에서 내부적으로 사용하는 BeanPropertyUtil에서
		 * Date형에 대해 getHours() 메소드를 호출하는데, java.sql.Date를 사용하면
		 * 해당 메소드(getHours())가 존재하지 않아 오류가 발생하게 된다.
		 */
//		java.sql.Date today = new java.sql.Date(cal.getTime().getTime());
		java.util.Date todayplus = new java.util.Date(cal.getTime().getTime());
		return todayplus;
	}

	/**
	 * 특정날짜에 plus만큼 연산을 한다.
	 * @param date
	 * @param plus
	 * @return
	 */
	public static java.util.Date dayDateMinus(java.util.Date date, int plus) {
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.setTime(date);
		cal.add(Calendar.DATE, plus * (-1));

		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		java.util.Date todayplus = new java.util.Date(cal.getTime().getTime());
		return todayplus;
	}

	/**
	 * 특정날짜에 plus만큼 연산을 한다.
	 * @param date
	 * @param plus
	 * @return
	 */
	public static java.util.Date dayDatePlus(java.util.Date date, int plus) {
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.setTime(date);
		cal.add(Calendar.DATE, plus);

		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		java.util.Date todayplus = new java.util.Date(cal.getTime().getTime());
		return todayplus;
	}
	public static java.sql.Date covertSqlDate(java.util.Date date) {
		return new java.sql.Date(date.getTime());
	}
	public static java.util.Date covertUtilDate(java.sql.Date date) {
		return new java.util.Date(date.getTime());
	}

	public static void main(String[] argv) {
		try{
			System.err.println(""+DateUtil.distance("2003-03-31", "2003-02-01"));
			System.err.println("todayDate() : "+todayDate());
			System.err.println(""+todayDatePlus(-10));
		}
		catch (Exception ex) {

		}
	}

	/**
	 *시간에  분리기호 추가
	 * @param value
	 * @return
	 */
	public static String addColon(String value) {
		if (value == null || value.length() < 6) return "";
		String result = null;
		result = value.substring(0, 2)+":"+ value.substring(2, 4)+":"+value.substring(4);
		return result;
	}

	/**
	 *날짜에  분리기호 추가
	 * @param time
	 * @return
	 */
	public static String addDot(String date) {
		if (date == null || date.length() < 8) return "";
		String result = null;
		result = date.substring(0, 4)+"."+date.substring(4, 6)+"."+date.substring(6);
		return result;
	}

	/**
	 * 날짜, 시간에 분리기호 추가
	 * @param val : 날짜 또는 시간
	 * @return
	 */
	public static String addSeparationMark(String value, String separation_mark) {
		if (value == null || value.length() < 6) return "";
		String result = null;
		if (value.length() == 8) { //4,2,2
			result = value.substring(0, 4)+ separation_mark +value.substring(4, 6)+ separation_mark + value.substring(6);
		} else {  //2,2,2
			result = value.substring(0, 2)+ separation_mark + value.substring(2, 4)+ separation_mark + value.substring(4);
		}
		return result;
	}
	
	/**
	 * 정수로 된 시간을 초단위(sec)로 입력 받아, "04:11:15" 등의 형식의 문자열로 시분초를 저장
	 * 
	 * @param secs
	 * @return
	 */
	public static String toHHMMSS(int secs) {
		int hour, min, sec;
		
		sec  = secs % 60;
		min  = secs / 60 % 60;
		hour = secs / 3600;

		return String.format("%02d:%02d:%02d", hour, min, sec);
	}
}

